package jukebox.controller;

import java.util.Calendar;
import java.util.List;
import javax.annotation.Resource;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import jukebox.model.common.ListVO;
import jukebox.model.member.MemberVO;
import jukebox.model.qnaboard.QnABoardService;
import jukebox.model.qnaboard.QnABoardVO;
import jukebox.model.qnacomment.QnACommentVO;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

@Controller
public class QnABoardController {
	
	@Resource(name="qnABoardServiceImpl")
	private QnABoardService qnaboardService;
	
	/**
	* @method : getList
	* @author : 최민석
	* @since : 2014-06-23
	* @param : int - 리스트의 페이지
	* @return : ModelAndView
	* @comment : 페이지별 게시글 목록
	*/
	@RequestMapping("getQnAList.ju")
	public ModelAndView getList(Integer page, String title){
		System.out.println("getQnAList");
		ListVO<QnABoardVO> listAndPage =
				qnaboardService.pagingContent(page, title);
		
		return new ModelAndView("list.qna", "listAndPage", listAndPage);
	}
	/**
	* @method : delete
	* @author : 최민석
	* @since : 2014-06-23
	* @param : QnABoardVO-게시물의vo,
	*  				HttpSession-로그인한 사람의 게시글인지 확인하기 위한 세션
	* @return : String
	* @comment : 게시글 삭제
	*/
	@RequestMapping("deleteQnA.ju")
	public String delete(int no) {
		qnaboardService.deleteContent(no);

		return "redirect:/getQnAList.ju";
	}
	/**
	* @method : getDetail
	* @author : 최민석
	* @since : 2014-06-23
	* @param : int-게시글 번호
	* @return : ModelAndView
	* @comment : 게시글 내용 보기(조회수 증가 안함)
	*/
	@RequestMapping("getQnADetail.ju")
	public ModelAndView getDetail(int no){
		QnABoardVO bvo = qnaboardService.findContentByNo(no);
		return new ModelAndView("detail.qna", "bvo", bvo);
	}
	/**
	* @method : getDetailIncHit
	* @author : 최민석
	* @since : 2014-06-23
	* @param : HttpServletRequest-쿠키를 가져오기 위한 요청객체,
	* 				HttpServletResponse-쿠키를 저장하기 위한 응답객체
	* @return : ModelAndView
	* @comment : 게시글 내용 보기(조회수 증가 함)
	 */
	@RequestMapping("getQnADetailIncHit.ju")
	public ModelAndView getDetailIncHit(HttpServletRequest request,
			int no, HttpServletResponse response) {
		System.out.println("getQnADetailIncHit");
		//int no = Integer.parseInt(request.getParameter("no"));
		Cookie[] cookies = request.getCookies();
		boolean isCookie = false;
		if(cookies!=null&&cookies.length>0){
			for(int i = 0; i<cookies.length; i++){
				if(cookies[i].getName().equals(no + "Cookie")&&
						cookies[i].getValue().equals("|" + no + "|")){
					System.out.println(cookies[i].getValue());
					isCookie = true;
					break;
				}
			}
		}

		/*System.out.println(cookieValue);
		if(cookieValue!=null&&cookieValue.equals("|" + no + "|"))
			isCookie=true;*/
		QnABoardVO bvo = null;
		if(isCookie){
			bvo = qnaboardService.findContentByNo(no);
		}else{
			bvo = qnaboardService.findContentByNoIncHit(no);
			response.addCookie(new Cookie(no + "Cookie", "|" + no + "|"));
		}
		return new ModelAndView("detail.qna", "bvo", bvo);
	}
	/**
	* @method : getDataToModify
	* @author : 최민석
	* @since : 2014-06-23
	* @param : int-게시글번호,
	* 				HttpSession-로그인한 사람의 게시글인지 확인하기 위한 세션
	* @return : ModelAndView
	* @comment : 수정하기위한 게시글 내용 가져오기
	*/
	@RequestMapping("getQnADataToModify.ju")
	public ModelAndView getDataToModify(int no) {
		QnABoardVO bvo = qnaboardService.findContentByNoToModify(no);
		return new ModelAndView("modify.qna", "bvo", bvo);
	}
	/**
	* @method : modify
	* @author : 최민석
	* @since : 2014-06-23
	* @param : QnABoardVO-게시글을 수정할 내용이 담긴 vo
	* @return : String
	* @comment : 게시글 수정
	*/
	@RequestMapping("modifyQnA.ju")
	public String modify(QnABoardVO vo){
		qnaboardService.updateContent(vo);
		return "redirect:/getQnADetail.ju?no=" + vo.getNo();
	}
	/**
	* @method : writingPage
	* @author : 최민석
	* @since : 2014-06-24
	* @param : 
	* @return : String
	* @comment : 게시글 작성페이지로 이동
	 */
	@RequestMapping(value="writingPage.ju")
	public String writingPage() {
		return "write.qna";
	}
	/**
	* @method : write
	* @author : 최민석
	* @since : 2014-06-23
	* @param : QnABoardVO-게시글을 작성할 내용이 담긴 vo,
	* 				HttpSession-작성자 이름을 정하기 위한 session
	* @return : String
	* @comment : 게시글 작성
	 */
	@RequestMapping(value="writeQnA.ju", method=RequestMethod.POST)
	public String write(QnABoardVO vo, HttpSession session) {
		/*vo.getMemberVO().setId(((MemberVO)session.getAttribute("mvo")).getId());
		vo.getMemberVO().setNickname(((MemberVO)session.getAttribute("mvo")).getNickname());*/
		int no = qnaboardService.writeContent(vo);
		return "redirect:/getQnADetail.ju?no=" + no;
	}
	
	/**
	* @method : getComment
	* @author : 최민석
	* @since : 2014-06-23
	* @param : int-게시글 번호
	* @return : List<QnACommentVO>
	* @comment : 댓글 목록 가져오기
	*/
	@RequestMapping(value="getCommentQnA.ju")
	@ResponseBody
	public ListVO<QnACommentVO> getComment(int boardNo, Integer page){
		System.out.println("getCommentQnA");
		ListVO<QnACommentVO> lvo = qnaboardService.findPagedCommentListByBoardNo(boardNo, page);
		System.out.println(lvo);
		

		/*Calendar today = Calendar.getInstance();
		System.out.println(today);*/
		
		return lvo;
	}
	/**
	* @method : writeComment
	* @author : 최민석
	* @since : 2014-06-23
	* @param : QnACommentVO-댓글 내용이 담긴 vo,
	* 				HttpSession-작성자 정보를 저장하기 위한 세션
	* @return : List<QnACommentVO>
	* @comment : 댓글 작성
	 */
	@RequestMapping(value="writeCommentQnA.ju")
	@ResponseBody
	public ListVO<QnACommentVO> writeComment(QnACommentVO vo, HttpSession session){
		System.out.println("writeCommentQnA");
		vo.setMemberVO((MemberVO)session.getAttribute("mvo"));
		System.out.println(vo);
		ListVO<QnACommentVO> lvo = qnaboardService.writeComment(vo);
		return lvo;
	}
	/**
	* @method : modifyComment
	* @author : 최민석
	* @since : 2014-06-23
	* @param : QnACommentVO-댓글 내용이 담긴 vo,
	* 				int-게시글번호
	* @return : List<QnACommentVO>
	* @comment : 댓글 수정
	*/
	@RequestMapping(value="modifyCommentQnA.ju")
	@ResponseBody
	public ListVO<QnACommentVO> modifyComment(QnACommentVO vo, Integer page){
		System.out.println("modifyCommentQnA");
		ListVO<QnACommentVO> lvo = qnaboardService.modifyComment(vo, page);
		return lvo;
	}
	/**
	* @method : deleteComment
	* @author : 최민석
	* @since : 2014-06-23
	* @param : QnACommentVO-댓글 내용이 담긴 vo,
	* 				int-게시글번호
	* @return : List<QnACommentVO>
	* @comment : 댓글 삭제
	*/
	@RequestMapping(value="deleteCommentQnA.ju")
	@ResponseBody
	public ListVO<QnACommentVO> deleteComment(QnACommentVO vo){
		System.out.println("deleteCommentQnA");
		ListVO<QnACommentVO> lvo = qnaboardService.deleteComment(vo.getNo(), vo.getBoardNo());
		return lvo;
	}
	/**
	* @method : choiceComment
	* @author : 최민석
	* @since : 2014-06-23
	* @param : QnACommentVO-댓글 내용이 담긴 vo,
	* 				int-게시글번호
	* @return : List<QnACommentVO>
	* @comment : 댓글 채택
	*/
	@RequestMapping(value="choiceCommentQnA.ju")
	@ResponseBody
	public ListVO<QnACommentVO> choiceComment(QnACommentVO vo, Integer page){
		System.out.println("choiceCommentQnA");
		ListVO<QnACommentVO> lvo = qnaboardService.choiceComment(vo.getNo(), vo.getBoardNo(), page);
		return lvo;
	}
}
